package com.caigou.modules.service.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.comm.base.widget.TipsDialog
import com.caigou.modules.service.adapter.HelpItemAdapter
import com.caigou.modules.service.baen.HelpItemBean
import com.caigou.modules.service.databinding.FragmentHelpBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/17
 */
@AndroidEntryPoint
class HelpFragment : BaseFragment() {

    private var _binding: FragmentHelpBinding? = null
    private val binding get() = _binding!!

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: HelpItemAdapter by lazy {
        HelpItemAdapter(mutableListOf())
    }

    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    private val list = mutableListOf<HelpItemBean>()

    private var tipsDialog: TipsDialog? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentHelpBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.helpRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            tipsDialog?.apply {
                dismiss()
                tipsDialog = null
            }
            when (position) {
                0, 1, 3, 5, 6 -> {
                    tipsDialog = TipsDialog(requireActivity(), false)
                    tipsDialog?.run {
                        tvTitle.text = list[position].title
                        tvContent.text = list[position].desc
                        tvRight.text = "我知道了"
                        tvRight.setOnClickListener {
                            dismiss()
                            tipsDialog = null
                        }
                        show()
                    }
                }
                2, 4 -> {
                    tipsDialog = TipsDialog(requireActivity(), false)
                    tipsDialog?.run {
                        tvTitle.text = list[position].title
                        tvContent.text = list[position].desc
                        tvRight.text = if (position == 2) {
                            "去每期资料"
                        } else {
                            "去高手论坛"
                        }
                        tvRight.setOnClickListener {
                            if (position == 2) {
                                ARouter.getInstance().build(RouterPath.Data.PAGE_DATA).navigation()
                            } else {
                                ARouter.getInstance().build(RouterPath.Forum.PAGE_FORUM).navigation()
                            }
                            dismiss()
                            tipsDialog = null
                        }
                        show()
                    }
                }
                7 -> {
                    tipsDialog = TipsDialog(requireActivity(), true)
                    tipsDialog?.run {
                        tvTitle.text = list[position].title
                        tvContent.text = list[position].desc
                        tvLeft.text = "取消"
                        tvLeft.setOnClickListener {
                            dismiss()
                            tipsDialog = null
                        }
                        tvRight.text = "去高手论坛"
                        tvRight.setOnClickListener {
                            ARouter.getInstance().build(RouterPath.Forum.PAGE_FORUM).navigation()
                            dismiss()
                            tipsDialog = null
                        }
                        show()
                    }
                }
            }
        }

        binding.helpTvBack.setOnClickListener {
            (requireActivity() as ServiceActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        list.add(HelpItemBean("什么是查询助手？", "指各种条件相关资料查询。"))
        list.add(HelpItemBean("什么是六合图库？", "指免费提供全年多钟六合彩图库。"))
        list.add(HelpItemBean("什么是全年玄机？", "指全年固定的六合彩玄机破解。"))
        list.add(HelpItemBean("什么是专家？", "指在六合彩方面有专门研究或特长的人，李咏经验为广大彩民朋友提供精准好料。"))
        list.add(HelpItemBean("什么是心水？", "指用户分享的档期购彩组合或资料"))
        list.add(HelpItemBean("什么是聊码大厅？", "用户可以针对当期心水进行聊天室的沟通。"))
        list.add(HelpItemBean("什么是老黄历？", "指中国传统农历历法，带有六合吉数和波色。"))
        list.add(HelpItemBean("APP会收费吗？", "本软件承诺不含有收费服务，所有服务【免费】提供，请放心使用"))
        mAdapter.setList(list)
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}